package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.rest.BaseController;
import com.cxf.windex.base.util.EncryptUtil;
import com.cxf.windex.biz.WeiUserBiz;
import com.cxf.windex.entity.WeiUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author chenxf
 * @date 3/8/2018
 * @description
 */
@Controller
@RequestMapping("user")
public class WeiUserContoller extends
    BaseController<WeiUserBiz,WeiUser> {


  @ApiOperation(value="登陆", notes="登陆返回TOKEN")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType="query", dataType = "String"),
      @ApiImplicitParam(name = "password", value = "密码", required = true,  paramType="query",dataType = "String")
  })
  @RequestMapping(value = "/login", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse login(String username,String password){

    WeiUser user=baseBiz.selectOne(new WeiUser(username));
    if(user==null){
        return  new ObjectRestResponse().rel(false).msg("用户不存在");
    }
    if(!EncryptUtil.decrypt(user.getPassword()).equals(password)){
      return  new ObjectRestResponse().rel(false).msg("密码不正确");
    }
    setCurrentUserName(user);
    return  new ObjectRestResponse().rel(true).msg("登陆成功");
  }

  @ApiOperation(value="登陆", notes="登陆返回TOKEN")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "password", value = "密码", required = true,  paramType="query",dataType = "String"),
      @ApiImplicitParam(name = "newPassword", value = "新密码", required = true,  paramType="query",dataType = "String"),
      @ApiImplicitParam(name = "newPassword2", value = "新密码2", required = true,  paramType="query",dataType = "String")

  })
  @RequestMapping(value = "/pwd", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse pwd(String password,String newPassword,String newPassword2){
    if(StringUtils.isBlank(password)){
      return  new ObjectRestResponse().rel(false).msg("密码不能为空");
    }

    if(StringUtils.isBlank(newPassword)){
      return  new ObjectRestResponse().rel(false).msg("新密码不能为空");
    }

    if(StringUtils.isBlank(newPassword2)){
      return  new ObjectRestResponse().rel(false).msg("重复新密码不能为空");
    }

    if(!newPassword.equals(newPassword2)){
      return  new ObjectRestResponse().rel(false).msg("重复新密码与新密码不相同");
    }
    String username=this.getCurrentUserName();
    if(StringUtils.isBlank(username)){
      return  new ObjectRestResponse().rel(false).msg("当前用户不存在");
    }
    WeiUser user=baseBiz.selectOne(new WeiUser(username));
    if(user==null){
      return  new ObjectRestResponse().rel(false).msg("用户不存在");
    }
    if(!EncryptUtil.decrypt(user.getPassword()).equals(password)){
      return  new ObjectRestResponse().rel(false).msg("密码不正确");
    }
    WeiUser pwduser=new WeiUser();
    pwduser.setId(user.getId());
    pwduser.setPassword(EncryptUtil.encrypt(newPassword));
    baseBiz.updateSelectiveById(pwduser);
    return  new ObjectRestResponse().rel(true).msg("修改成功");
  }

}
